﻿@{
    Layout = null;
}
@if (false)
{
    <script src="~/Scripts/jquery-1.9.1.intellisense.js"></script>
}
<script src="@Url.Content("~/Scripts/tinymce/tiny_mce.js")" type="text/javascript"></script>
<script type="text/javascript">

    $(document).ready(function () {
        $("#LabelInlineEditingWindow").dialog({
            autoOpen: false,
            resizable: true,
            height: 460,
            width: 820,
            modal: true,
            buttons: {
                Ok: function () {
                    save();
                    $(this).dialog("close");
                },
                Cancel: function () {
                    cancel();
                    $(this).dialog("close");
                }
            }
        });

        //it adds button next to inputs for validation popup
        $(window).load(function () {


            $('.jquery-custom-selectboxes-replaced').before("<div class='validationButton' value='val' onclick='ValidationEditor(this)' ></div>")
            $('input[data-val-hasval][type!="hidden"][type!="radio"][type!="checkbox"]').before("<div class='validationButton' value='val' onclick='ValidationEditor(this)' ></div>")
            $('[type="checkbox"][data-val-hasval-entityname!="Client"]').before("<div class='validationButtonChkbox ' value='val' onclick='ValidationEditor(this)' ></div>")
            $('[type="checkbox"][data-val-hasval-entityname="Client"]').before("<div class='validationButtonClientEntity' value='val' onclick='ValidationEditor(this)' ></div>")
            $('.TelerikComBoVal').before("<div class='validationButtonCombo' value='val' onclick='ValidationEditor(this)' ></div>")
            $('.passiveAccountDiv').css('z-index', '0', 'important')

        });


        $('.localizedText').hover(function () {
            $(this).attr("style", "border:1px solid red;");
        });

        $('.localizedText').click(function (e) {
            e.preventDefault();
        });

        $('.localizedText').mouseleave(function () {

            $(this).removeAttr("style");
        });


        $('.localizedText').dblclick(function () {

            var html = $(this).html();
            $(this).attr("isActive", "true");
            var ed = tinyMCE.get('htmleditor');
            ed.setContent(html);
            $("#LabelInlineEditingWindow").dialog("open")
        });


        $('.localizedStaticContent').dblclick(function myfunction() {
            var html = $(this).html();
            $(this).attr("isActive", "true");
            $(this).attr("IsStaticContent", "true");
            var ed = tinyMCE.get('htmleditor');
            ed.setContent(html);
            $("#LabelInlineEditingWindow").dialog("open")
        });


        // Content Holder start
        $("#addPosition").click(function () {
            var html = $("#positionContentHolderTemplateId").html();
            $("#positionContentHolder").append("<far class='ui-state-default'>" + html + "</far>");

        });

        $('.addContent').click(function (e) {
            e.preventDefault();
            var html = $("#positionContentHolderTemplateId").html();
            var pageid = $(this).parents(".localizedPosition").attr("pageId");
            var holder = $(this).parents(".localizedPosition");
            var holderid = $(this).parents(".localizedPosition").attr("holderid");

            $.ajax({
                type: "post",
                url: '@Url.Action("AddStaticContent", "InlineEditing", new { area = "Admin" })',
                data: {
                    pageId: pageid, holderId: holderid
                },
                dataType: "html",
                success: function (msg) {
                    var retVal = eval('(' + msg + ')')

                    $(holder).append(jQuery.validator.format(html,
                       retVal.key, pageid, retVal.ordid, holderid, retVal.partid));
                    //key='{0}' pageId='{1}' ordid='{2}' holderid='{3}' partid='{4}'
                },
                error: function (msg) {

                    alert('Error on inserting new content');
                }
            });
        });

        $('.addView').click(function (e) {

            e.preventDefault();

            $(this).parents(".localizedPosition").attr("isActiveSelectedView", true);
            //                var html = $("#positionContentHolderTemplateId").html();

            //                var pageid = $(this).parents(".localizedPosition").attr("pageId");
            //                var holderid = $(this).parents(".localizedPosition").attr("holderid");

            $('#ViewSelectorWindow').data('tWindow').center().open();
        });
        //$("a.foo").live("click", fn)
        //$(document).on("click", "a.foo", fn)
        //$(".positionContentHolderRemove").live('click', function () {
        $(document).on("click", ".positionContentHolderRemove", function () {
            var obj = $(this);
            jConfirm('Are you Sure?', 'Delete Content', function (r) {
                if (r == true) {
                    var pageid = $(obj).parents(".ContentHolderHeader").next().next("far").attr("pageid");
                    var partid = $(obj).parents(".ContentHolderHeader").next().next("far").attr("partid");
                    $.ajax({
                        type: "post",
                        url: '@Url.Action("RemovePart", "InlineEditing", new { area = "Admin" })',
                        data: {
                            pageId: pageid, partId: partid
                        },
                        dataType: "html",
                        success: function (msg) {
                            $(obj).parents(".ContentHolderHeader").next().next("far").remove();
                            $(obj).parents(".ContentHolderHeader").next().remove();
                            $(obj).parents(".ContentHolderHeader").remove();
                        },
                        error: function (msg) {

                            alert('Error on deleting content');
                        }
                    });

                }
            });


        });

        //$(".positionContentHolderEdit").live('click', function () {
        $(document).on("click", ".positionContentHolderEdit", function () {
            var htmlContent = $(this).parents(".ContentHolderHeader").next().next("far");
            var positionContent = htmlContent.html();
            $(this).attr("isActive", "true");
            htmlContent.attr("IsStaticContent", "true");
            htmlContent.attr("isActive", "true");

            var ed = tinyMCE.get('htmleditor');
            ed.setContent(positionContent);

            $("#LabelInlineEditingWindow").dialog("open")
        });

        //$(".positionContentHolderToggle").live('click', function () {
        $(document).on("click", ".positionContentHolderToggle", function () {
            $(this).parents(".ContentHolderHeader").next().next("far").toggle();
        });

        // Content Holder End
    })

    function cancel() {
        $('.positionContentHolderEdit[isActive="true"]').removeAttr('isActive');
        $('.localizedText[isActive="true"]').removeAttr('isActive');
        if ($('.localizedStaticContent[IsStaticContent="true"]').length > 0) {
            $('.localizedStaticContent[IsStaticContent="true"]').removeAttr('IsStaticContent');
        }
        if ($('.localizedStaticContent[isActive="true"]').length > 0) {
            $('.localizedStaticContent[isActive="true"]').removeAttr('isActive');

        }
    }

    //Insert or update the value of labels or static contents
    function save() {
        var ed = tinyMCE.get('htmleditor');
        var html = ed.getContent();
        //Position
        var htmlContent = $('.positionContentHolderEdit[isActive="true"]').parents(".ContentHolderHeader").next().next("far");
        htmlContent.html(html);

        if (html == null || html == '') {
            html = 'Double click to edit.';
        }

        if ($('.localizedStaticContent[IsStaticContent="true"]').length == 0) {
            $.ajax({
                type: "post",
                url: '@Url.Action("UpdateLabel", "InlineEditing", new { area = "Admin" })',
                data: {
                    key: $('.localizedText[isActive="true"]').attr('key'), value: html, countrySiteId: $('.localizedText[isActive="true"]').attr('countrysiteid')
                },
                dataType: "html",
                success: function (msg) {
                    $('.localizedText[isActive="true"]').html(html);
                    $('.localizedText[isActive="true"]').removeAttr('isActive');
                },
                error: function (msg) {
                    $('.localizedText[isActive="true"]').removeAttr('isActive');
                    alert('Error on saving label!');
                }
            });
        }
        else if ($('.localizedStaticContent[IsStaticContent="true"]').length > 0) {
            $.ajax({
                type: "post",
                url: '@Url.Action("UpdateStaticContent", "InlineEditing", new { area = "Admin", })',
                data: {
                    key: $('.localizedStaticContent[isActive="true"]').attr('key'), value: html, pageId: $('.localizedStaticContent[isActive="true"]').attr('pageId')
                },
                dataType: "html",
                success: function (msg) {
                    $('.localizedStaticContent[isActive="true"]').html(html);
                    $('.localizedStaticContent[isActive="true"]').removeAttr('isActive');
                    $('.localizedStaticContent[IsStaticContent="true"]').removeAttr('IsStaticContent');
                },
                error: function (msg) {
                    $('.localizedStaticContent[isActive="true"]').removeAttr('isActive');
                    $('.localizedStaticContent[IsStaticContent="true"]').removeAttr('IsStaticContent');
                    alert('Error on saving label!');
                }
            });
        }
    if ($('.positionContentHolderEdit[isActive="true"]').length > 0) {
        $('.positionContentHolderEdit[isActive="true"]').removeAttr('isActive');
    }
}

function SelectView(id) {
    //alert(id);
    // $('#ViewSelectorWindow').data('tWindow').close();

    var position = $('.localizedPosition[isActiveSelectedView="true"]');
    var html = $("#positionDynamicContentHolderTemplateId").html();

    var pageid = $(position).attr("pageId");
    var holderid = $(position).attr("holderid");
    //alert(pageid + ' ' + holderid + ' ' + id);

    $.ajax({
        type: "post",
        url: '@Url.Action("AddDynamicContent", "InlineEditing", new { area = "Admin" })',
        data: {
            pageId: pageid, holderId: holderid, viewId: id
        },
        dataType: "html",
        success: function (msg) {
            var retVal = eval('(' + msg + ')')

            $(position).append(jQuery.validator.format(html,
                   retVal.key, pageid, retVal.ordid, holderid, retVal.partid));
            renderUrl(retVal.view);

            $('.localizedPosition[isActiveSelectedView="true"]').removeAttr('isActiveSelectedView');
        },
        error: function (msg) {
            $('.localizedPosition[isActiveSelectedView="true"]').removeAttr('isActiveSelectedView');
            alert('Error on adding view');
        }
    });

}

function renderUrl(url) {
    var result = '';
    $.ajax({
        type: "post",
        url: url,
        data: '',
        dataType: "html",
        success: function (msg) {

            $('.localizedDynamicContent[needContent="true"]').append(msg);
            $('.localizedDynamicContent[needContent="true"]').removeAttr('needContent');
        },
        error: function (msg) {
            return 'Error on loading view';
        }
    });

}

function ValidationEditor(e) {
    var field = $(e).next();
    if (field.hasClass('TelerikComBoVal')) {
        field = $(e).next().find('input[data-val-hasval]');
    }
    var name = field.attr('name');
    var entityname = field.attr('data-val-hasval-entityname');
    var pagename = field.attr('data-val-hasval-pagename');
    var type = field.get(0).tagName;
    var ischeckbox = field.attr('class');
    var type = field.attr('class') == 'checkbox' ? $(e).next().attr('class') : $(e).next().get(0).tagName;
    var countrySiteId = field.attr('data-val-hasval-countrysiteid');

    $.ajax({
        type: "post",
        url: '@Url.Action("Index", "ValidationEditor", new { area = "Admin" })',
        data: { entityname: entityname, pagename: pagename, name: name, type: type, countrySiteId: countrySiteId },
        dataType: "html",
        success: function (msg) {
            $("#ValidationEditorResult").html('');
            $("#ValidationEditorResult").append(msg);

            $("#ValidationEditorResult").lightbox_me();
        },
        error: function (msg) {

        }
    });
}
</script>
<div id="LabelInlineEditingWindow" title="Content Editor" style="display: none">
    @Html.Partial("RichEditorView", string.Empty)
</div>
